package org.vectrics.mail.struts;

import java.util.Date;

import javax.servlet.jsp.JspException;

import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsActionData;
import org.vectrics.struts.VectricsStrutsAction;
import org.vectrics.user.Person;

public class SaveRecipientEmailAction extends VectricsStrutsAction {

	public SaveRecipientEmailAction() {
		super();
	}

	@Override
	protected String doExecute(VectricsActionData actionData)
			throws JspException {
		log.info("Saving template text");
		
		
		Long recipientId = null;
		String recipientIdStr = actionData.getRequest().getParameter("recipientId");
		recipientId = new Long(recipientIdStr);
		Person recipient = CoreServiceLocator.getPersonService().findPersonById(recipientId); 
		
		String senderIdStr = actionData.getRequest().getParameter("senderId");
		Long senderId = new Long(senderIdStr);
		Person sender = CoreServiceLocator.getPersonService().findPersonById(senderId); 

		String stageMillisStr = actionData.getRequest().getParameter("stageTime");
		Long stageMillis = new Long(stageMillisStr);
		Date stageDate = new Date(stageMillis);
		
		String letter = actionData.getRequest().getParameter("body");
		String subject = actionData.getRequest().getParameter("subject");
		
		LetterStagingManager stagingManager = new LetterStagingManager(sender, stageDate);
		stagingManager.saveBody(recipient, letter);
		stagingManager.saveSubject(recipient, subject);
		
		return "success";
	}

}
